package www.openteam.com.ui.realtime;


import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.getbase.floatingactionbutton.FloatingActionButton;
import com.getbase.floatingactionbutton.FloatingActionsMenu;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import www.openteam.com.R;
import www.openteam.com.adapter.realtime.EvaAMadp;
import www.openteam.com.adapter.realtime.EvaPaperAdp;
import www.openteam.com.adapter.realtime.EvaPaperPitAdp;
import www.openteam.com.adapter.realtime.EvalutateAddOrderAdp;
import www.openteam.com.adapter.realtime.EvalutatePrtPrcAdp;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.EvaAMbean;
import www.openteam.com.bean.EvaPaperBean;
import www.openteam.com.bean.EvaPaperPitBean;
import www.openteam.com.bean.EvaPrtPrcBean;
import www.openteam.com.bean.Evaluate_customer;
import www.openteam.com.bean.EvalutateAddOrderBean;
import www.openteam.com.bean.MoneryTypeWithRate;
import www.openteam.com.bean.PbKzMsg;
import www.openteam.com.bean.PbMaterial;
import www.openteam.com.bean.ProductMsg;
import www.openteam.com.contract.realtime.EvaluateContract;
import www.openteam.com.presenter.business.EvaluatePresenter;
import www.openteam.com.tool.TimeTool;
import www.openteam.com.tool.ToastUtil;


/**
 * 新版估价
 */
public class EvaluateFragment extends mBaseFragment<EvaluatePresenter> implements
        EvaluateContract.EvaluateView {


    @Bind(R.id.evaluate_orderRecly)
    RecyclerView evaluateOrderRecly;
    @Bind(R.id.evaluate_prtPrdRecly)
    RecyclerView evaluatePrtPrdRecly;
    @Bind(R.id.evaluate_paperTableRelcy)
    RecyclerView evaluatePaperTableRelcy;
    @Bind(R.id.evaluate_pitPapleRecly)
    RecyclerView evaluatePitPapleRecly;
    @Bind(R.id.evaluate_auxMaterialRecly)
    RecyclerView evaluateAuxMaterialRecly;
    @Bind(R.id.evaluate_btn_orderNumb)
    RadioButton evaluateBtnOrderNumb;
    @Bind(R.id.evaluate_productProcess)
    RadioButton evaluateProductProcess;
    @Bind(R.id.evaluate_paperTable)
    RadioButton evaluatePaperTable;
    @Bind(R.id.evaluate_pitPaple)
    RadioButton evaluatePitPaple;
    @Bind(R.id.evaluate_auxMaterial)
    RadioButton evaluateAuxMaterial;
    @Bind(R.id.evaluate_type)
    AppCompatSpinner evaluateType;
    @Bind(R.id.evaluate_search)
    ImageButton evaluateSearch;
    @Bind(R.id.evaluate_custom_type)
    AppCompatSpinner evaluateCustomType;
    @Bind(R.id.evaluate_delivery_date)
    TextView evaluateDeliveryDate;
    @Bind(R.id.evaluate_product)
    EditText evaluateProduct;
    @Bind(R.id.evaluate_width)
    EditText evaluateWidth;
    @Bind(R.id.evaluate_high)
    EditText evaluateHigh;
    @Bind(R.id.evaluate_bag_top_postion)
    EditText evaluateBagTopPostion;
    @Bind(R.id.evaluate_welt_postion)
    EditText evaluateWeltPostion;
    @Bind(R.id.evaluate_unitPrice)
    EditText evaluateUnitPrice;
    @Bind(R.id.evaluate_totalPrice)
    EditText evaluateTotalPrice;
    @Bind(R.id.evaluate_taxes)
    EditText evaluateTaxes;
    @Bind(R.id.evaluate_taxRate)
    EditText evaluateTaxRate;
    @Bind(R.id.evaluate_remarks)
    EditText evaluateRemarks;
    @Bind(R.id.evaluate_msgEdit_layout)
    LinearLayout evaluateMsgEditLayout;
    @Bind(R.id.evaluate_examine)
    FloatingActionButton evaluateExamine;
    @Bind(R.id.evaluate_unexamine)
    FloatingActionButton evaluateUnexamine;
    @Bind(R.id.evaluate_delete)
    FloatingActionButton evaluateDelete;
    @Bind(R.id.evaluate_keep)
    FloatingActionButton evaluateKeep;
    @Bind(R.id.multiple_actions)
    FloatingActionsMenu multipleActions;


    List<RecyclerView> recyclerViewList;
    EvalutateAddOrderAdp evalutateAddOrderAdp;//订单添加的adp
    EvalutatePrtPrcAdp evalutatePrtPrcAdp;//生产工艺的adp
    EvaPaperAdp evaPaperAdp;//纸张adp
    EvaPaperPitAdp evaPaperPitAdp;//坑纸adp
    EvaAMadp evaAMadp;

    List<EvalutateAddOrderBean> addOrderList;
    List<EvaPrtPrcBean> prtPrcList;
    List<EvaPaperBean> paperList;
    List<EvaPaperPitBean> paperPitList;
    List<EvaAMbean> amList;
    List<String> numbList;//复数订单数量的集和,生产工艺和纸张表格信息表都要用到，同一个
    boolean isLoad = true;//标记numbList是否在添加订单数量处进行了修改


    public EvaluateFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_evaluate, container, false);
        ButterKnife.bind(this, view);
        evaluateOrderRecly.setLayoutManager(new LinearLayoutManager(getContext()));
        mPresenter = loadPresenter();
        initData();
        initReclyView();
        initView();
        initListener();
        return view;
    }


    protected void initData() {
        if (mPresenter != null)
            mPresenter.attachView(this);

        List<String> operatoList = new ArrayList<>();
        operatoList.add("骑订书估价");
        operatoList.add("胶装书估价");
        operatoList.add("精装书估价");
        operatoList.add("手挽袋估价");
        operatoList.add("包装盒估价");
        operatoList.add("一般估价");
        operatoList.add("天地盒估价");
        ArrayAdapter<String> operatoAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, operatoList);
        evaluateType.setAdapter(operatoAdapter);


    }

    @Override
    protected void initListener() {
        String stype = mySetting.getString("rCode", "");
        int userid = mySetting.getInt("mId", 0);
        evaluateSearch.setOnClickListener(v -> {
            mPresenter.findCustomerCode();
            mPresenter.findPbMonerytype();
            mPresenter.findProductMsg(stype, userid);
            mPresenter.findPbKzMsg();
            mPresenter.findPbMaterial();
        });
        evaluateDeliveryDate.setOnClickListener(v -> {
            evaluateDeliveryDate.setTag(evaluateDeliveryDate);
            TimeTool.selectCalendar(getContext(), evaluateDeliveryDate);
        });


        evaluateKeep.setOnClickListener(view1 -> {
            //提交
        });

        evaluateExamine.setOnClickListener(view1 -> {
            //审核
        });

        evaluateUnexamine.setOnClickListener(view1 -> {
            //反审核
        });

        evaluateDelete.setOnClickListener(view1 -> {
            //删除
        });

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected EvaluatePresenter loadPresenter() {
        return new EvaluatePresenter();
    }

    private void initReclyView() {
        evaluateOrderRecly.setLayoutManager(new LinearLayoutManager(getContext()));
        evaluatePrtPrdRecly.setLayoutManager(new LinearLayoutManager(getContext()));
        evaluatePaperTableRelcy.setLayoutManager(new LinearLayoutManager(getContext()));
        evaluatePitPapleRecly.setLayoutManager(new LinearLayoutManager(getContext()));
        evaluateAuxMaterialRecly.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerViewList = new ArrayList<>();
        recyclerViewList.add(evaluateOrderRecly);
        recyclerViewList.add(evaluatePrtPrdRecly);
        recyclerViewList.add(evaluatePaperTableRelcy);
        recyclerViewList.add(evaluatePitPapleRecly);
        recyclerViewList.add(evaluateAuxMaterialRecly);
        evalutateAddOrderAdp = null;
        evalutatePrtPrcAdp = null;
        evaPaperAdp = null;
        evaPaperPitAdp = null;
        evaAMadp = null;
        evaluateBtnOrderNumb.setChecked(true);
        if (evalutateAddOrderAdp == null) {
            evalutateAddOrderAdp = new EvalutateAddOrderAdp(getContext());
        }
        evaluateOrderRecly.setAdapter(evalutateAddOrderAdp);
        addOrderList = evalutateAddOrderAdp.getCallBackList();
        isLoad = true;
        evaluateOrderRecly.setVisibility(View.VISIBLE);
        addOrderList = new ArrayList<>();
        prtPrcList = new ArrayList<>();
        paperList = new ArrayList<>();
        paperPitList = new ArrayList<>();
        amList = new ArrayList<>();
        numbList = new ArrayList<>();
    }

    private void setReclyInVisible() {
        //设置recycleView隐藏
        if (null != recyclerViewList || recyclerViewList.size() > 0) {
            int size = recyclerViewList.size();
            for (int i = 0; i < size; i++) {
                recyclerViewList.get(i).setVisibility(View.GONE);
            }
        }
    }


    @OnClick({R.id.evaluate_btn_orderNumb, R.id.evaluate_productProcess, R.id.evaluate_paperTable, R.id.evaluate_pitPaple, R.id.evaluate_auxMaterial})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.evaluate_btn_orderNumb:
                //订单数量
                setReclyInVisible();
                evaluateOrderRecly.setVisibility(View.VISIBLE);
                if (evalutateAddOrderAdp == null) {
                    evalutateAddOrderAdp = new EvalutateAddOrderAdp(getContext());
                }
                evaluateOrderRecly.setAdapter(evalutateAddOrderAdp);
                addOrderList = evalutateAddOrderAdp.getCallBackList();
                isLoad = true;
                break;
            case R.id.evaluate_productProcess:
                //生产工艺
                setReclyInVisible();
                evaluatePrtPrdRecly.setVisibility(View.VISIBLE);
                if (isLoad) {
                    numbList = new ArrayList<>();
                    isLoad = false;
                }
                if (evalutatePrtPrcAdp == null) {
                    evalutatePrtPrcAdp = new EvalutatePrtPrcAdp(getContext());
                }
                evaluatePrtPrdRecly.setAdapter(evalutatePrtPrcAdp);
                prtPrcList = evalutatePrtPrcAdp.getBeanList();
                if (addOrderList == null) {
                    numbList.add("0");
                } else {
                    for (EvalutateAddOrderBean bean : addOrderList) {
                        numbList.add(bean.getNumb() + "");
                    }
                }
                evalutatePrtPrcAdp.setNumbList(numbList);
                break;
            case R.id.evaluate_paperTable:
                //纸张
                setReclyInVisible();
                evaluatePaperTableRelcy.setVisibility(View.VISIBLE);
                //避免不必要的重复初始化numblist
                if (isLoad) {
                    initNumbList();
                }
                if (evaPaperAdp == null) {
                    evaPaperAdp = new EvaPaperAdp(getContext());
                }
                evaluatePaperTableRelcy.setAdapter(evaPaperAdp);
                paperList = evaPaperAdp.getBeanList();
                evaPaperAdp.setNumbList(numbList);
                break;
            case R.id.evaluate_pitPaple:
                //坑纸
                setReclyInVisible();
                evaluatePitPapleRecly.setVisibility(View.VISIBLE);
                if (evaPaperPitAdp == null) {
                    evaPaperPitAdp = new EvaPaperPitAdp(getContext());
                }

                evaluatePitPapleRecly.setAdapter(evaPaperPitAdp);
                paperPitList = evaPaperPitAdp.getBeanList();
                break;
            case R.id.evaluate_auxMaterial:
                //辅料
                setReclyInVisible();
                evaluateAuxMaterialRecly.setVisibility(View.VISIBLE);
                if (isLoad) {
                    initNumbList();
                }
                if (evaAMadp == null) {
                    evaAMadp = new EvaAMadp(getContext());
                }
                evaAMadp.setNumbList(numbList);
                evaluateAuxMaterialRecly.setAdapter(evaAMadp);
                amList = evaAMadp.getBeanList();
                break;
        }
    }

    private void initNumbList() {
        //isLoad为true表示从订单数量模块过来的，订单数量可能进行了修改，初始化numblist
        numbList.clear();
        if (addOrderList == null) {
            numbList.add("0");
        } else {
            for (EvalutateAddOrderBean bean : addOrderList) {
                numbList.add(bean.getNumb() + "");
            }
        }
        isLoad = false;
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void findCustomerCodeSucess(Evaluate_customer evaluate_customer) {
        ToastUtil.showToast(getContext(), evaluate_customer.getInfo());
        List<Evaluate_customer.DataBeanX.DataBean> data = evaluate_customer.getData().getData();
        List<String> customerList = new ArrayList<>();
        for (Evaluate_customer.DataBeanX.DataBean datum : data) {
            customerList.add(datum.getName());
        }
        ArrayAdapter<String> customerAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, customerList);
        customerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        evaluateCustomType.setAdapter(customerAdapter);
    }

    @Override
    public void findPbMoneryType(MoneryTypeWithRate moneryTypeWithRate) {
        List<MoneryTypeWithRate.DataBeanX.DataBean> moneryTypeList = moneryTypeWithRate.getData().getData();
        evalutateAddOrderAdp.setMoneryTypeWithList(moneryTypeList);
    }

    @Override
    public void findProductMsg(ProductMsg productMsg) {
        List<ProductMsg.DataBeanX.DataBean> data = productMsg.getData().getData();
        if (evalutatePrtPrcAdp == null) {
            evalutatePrtPrcAdp = new EvalutatePrtPrcAdp(getContext());
        }
        evalutatePrtPrcAdp.setProductMsgList(data);
    }

    @Override
    public void findPbMaterial(PbMaterial pbMaterial) {
        List<PbMaterial.DataBeanX.DataBean> databeans = pbMaterial.getData().getData();
        if (evaAMadp == null) {
            evaAMadp = new EvaAMadp(getContext());
        }

    }

    @Override
    public void findPbKzMsg(PbKzMsg pbKzMsg) {
        List<PbKzMsg.DataBeanX.DataBean> dataBeans = pbKzMsg.getData().getData();
        if (evaPaperPitAdp == null) {
            evaPaperPitAdp = new EvaPaperPitAdp(getContext());
        }
        evaPaperPitAdp.setPitPaperList(dataBeans);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        mPresenter = null;
        addOrderList = null;
        prtPrcList = null;
        paperList = null;
        paperPitList = null;
        amList = null;

    }

}
